//
//  BrandViewModel.swift
//  shop
//
//  Created by chen on 2025/5/18.
//

import SwiftUI

@MainActor
class BrandViewModel: ObservableObject {
    
    public init() {
        self.loadMap = self.brandType.reduce(into: [:]) { result, item in
            result[item.type] = false
        }
    }
    
    @Published var brandType: [BrandType] = [
        BrandType(type: 6, name: "美食"),
        BrandType(type: 4, name: "居家日用"),
        BrandType(type: 3, name: "美妆"),
        BrandType(type: 8, name: "数码家电"),
        BrandType(type: 5, name: "鞋品"),
        BrandType(type: 9, name: "男装"),
        BrandType(type: 1, name: "女装"),
        BrandType(type: 10, name: "内衣"),
        BrandType(type: 14, name: "家装家纺"),
        BrandType(type: 7, name: "文娱车品"),
        BrandType(type: 12, name: "配饰"),
        BrandType(type: 11, name: "箱包"),
        BrandType(type: 13, name: "户外运动")
    ]
    
    @Published var brandMap: [Int:[BrandItem]] = [:]
    
    @Published var loadMap: [Int:Bool] = [:]
    
    public func getBrandMap(type: Int) {
        Task{
            loadMap[type] = true
            
            let queryParam:[String:Any] = [
                "version": "v1.0.0",
                "cid": type,
                "pageId": 1,
                "pageSize": 50
            ]
            
            await NetManager.shared.httpGet(.brandList, queryParam: queryParam) { (data: BrandResp) in
                self.loadMap[type] = false
                self.brandMap[type] = data.lists
            }
        }
    }
}
